<template>
	<view>
		<view class="d-flex justify-content-between flex-wrap" v-if="list.length>0">
			<cos-task-item v-for="(item,index) in list" :key="item.id" :item="item"></cos-task-item>
		</view>
		<!-- 加载状态 -->
		<block v-if="list.length === 0 && loadStatus == 'nomore'">
			<u-empty margin-top="100" text="暂无通告" mode="favor"></u-empty>
		</block>
		<block v-else>
			<view style="margin: 30rpx 0;">
				<u-loadmore :status="loadStatus"/>
			</view>
		</block>
	</view>
</template>

<script>
	import cosTaskItem from '../../components/cos-media/cos-task-item.vue';
	export default {
		components: {
			cosTaskItem
		},
		name: 'post-task-list',
		props: {
			list: Array,
			loadStatus: String,
			handle: {
				default: false,
				type: Boolean
			},
			showTopic: {
				default: true,
				type: Boolean
			},
			uid: Number,
			admin: {
				default: false,
				type: Boolean
			}
		},
		data() {
			return {
				showAction: false,
				indexPostAd: this.$c.indexPostAd,
				actionList: [{
						text: '关注TA',
						key: 'follow'
					},
					{
						text: '删除',
						color: 'red',
						key: 'delete'
					}
				],
				choosePost: '',
				chooseIndex: '',
				sessionUid: ''
			};
		},
		watch: {
			uid(n) {
				if (this.sessionUid == n) {
					this.actionList.unshift({
						text: '置顶',
						key: 'top'
					});
				}
			}
		},
		created() {
			let userInfo = uni.getStorageSync('userInfo');
			if (userInfo) {
				this.sessionUid = userInfo.uid;
			}
		},
		methods: {
			onShare() {},
			onActive(postInfo, index) {
				this.showAction = true;
				this.choosePost = postInfo;
				this.chooseIndex = index;
			},
			actionCon(index) {
				let key = this.actionList[index].key;
				if (key == 'follow') {
					this.follow();
				}

				if (key == 'delete') {
					this.postDel();
				}

				if (key == 'top') {
					this.postTop();
				}
			},
			cancelCollection(id, index) {
				this.$H
					.post('post/cancelCollection', {
						id: id
					})
					.then(res => {
						if (res.code === 200) {
							this.list[index].is_collection = false;
							this.list[index].collection_count--;
						}
					});
			},
			addCollection(id, index) {
				this.$H
					.post('post/addCollection', {
						id: id,
						uid: this.list[index].uid
					})
					.then(res => {
						if (res.code === 200) {
							this.list[index].is_collection = true;
							this.list[index].collection_count++;
						}
					});
			},
			// 置顶帖子
			postTop() {
				this.$H
					.post('post/setTop', {
						post_id: this.choosePost.id,
						topic_id: this.choosePost.topic_id
					})
					.then(res => {
						if (res.code == 200) {
							this.$q.toast('成功设为置顶', 'success');
						}
					});
			},
			postDel() {
				this.$H
					.post('post/del', {
						id: this.choosePost.id
					})
					.then(res => {
						if (res.code == 200) {
							this.list.splice(this.chooseIndex, 1);
						}
					});
			},
			follow() {
				this.$H
					.post('user/addFollow', {
						id: this.choosePost.uid
					})
					.then(res => {
						this.$u.toast(res.msg);
					});
			},
			previewImage(url, urls) {
				let images = [];
				if(urls.length>0) {
					urls.forEach(item=>{
						images.push(item.url)
					})
				}
				
				uni.previewImage({
					current: url, // 当前显示图片的http链接
					urls: images // 需要预览的图片http链接列表
				});
			},
			previewMedia(media, images, video) {
				let medias = [];
				if(video) {
					medias.push({
						url:video.url,
						type:'video',
						poster:video.poster.path
					})
				}
				
				if(images) {
					images.forEach(item=>{
						medias.push({
							url:item.url,
							type:'image',
						})
					})
				}
				
				let current = 0;
				medias.forEach((item, index)=>{
					if(item.url==media) {
						current = index
					}
				})
				uni.previewMedia({
					sources: medias, // 需要预览的资源列表
					current: current // 当前显示的资源序号
				})
			},
			jumpTopic(id) {
				uni.navigateTo({
					url: '/pages/post/shoot-detail?id=' + id
				});
			},
			jump(item) {				
				uni.navigateTo({
					url: '/pages/post/shoot-detail?id=' + item.id
				});

				// // 图文
				// if (e.type == 1 || e.type == 4) {
				// 	url = '/pages/post/detail?id=' + e.id;
				// }

				// //视频
				// if (e.type == 2) {
				// 	url = '/pages/post/video-detail?id=' + e.id;
				// }

				// uni.navigateTo({
				// 	url: url
				// });
			},
			toDiscuss(e) {
				uni.navigateTo({
					url: '/pages/discuss/detail?id=' + e.currentTarget.dataset.id
				});
			},
			toUcenter(uid) {
				uni.navigateTo({
					url: '/pages/user/home?uid=' + uid
				});
			}
		}
	};
</script>
